// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
@file:Suppress("HardCodedStringLiteral", "DialogTitleCapitalization")

package com.intellij.ide.startup.importSettings.testActions

import com.intellij.ide.IdeBundle
import com.intellij.ide.startup.importSettings.data.NotificationData
import com.intellij.ide.startup.importSettings.data.SettingsService
import com.intellij.ide.startup.importSettings.data.SyncService
import com.intellij.ide.startup.importSettings.data.TestSyncService
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.project.DumbAwareAction
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.dsl.builder.panel
import com.jetbrains.rd.util.lifetime.Lifetime
import com.jetbrains.rd.util.reactive.Property
import javax.swing.JComponent

class OnboardingTestDialogAction : DumbAwareAction() {
  override fun actionPerformed(e: AnActionEvent) {
    val settService = SettingsService.getInstance()
    val error = settService.notification

    if(error !is Property) return

    val dialog = object : DialogWrapper(null) {
      init {
        init()
      }

      override fun createCenterPanel(): JComponent {
        val s = "com.intellij.o penapi. project.impl .ProjectManag erImplKt.acc essche ckOl \ndTrusted Sta teAnd Mi grate (ProjectManag erImpl.kt:1)\n" +
                "\tat com.intellij.openap i.project.i mpl.ProjectManagerImplKti nitProje ct.inv okeSuspend(ProjectManagerImpl.kt:1233)"

        val pane = panel {
          row {
            button( "Error With Actions") {
              error.set(object : NotificationData {
                override val status: NotificationData.NotificationStatus = NotificationData.NotificationStatus.ERROR
                override val message: String = s

                override val customActionList: List<NotificationData.Action> = arrayListOf(
                  NotificationData.Action("Skip") {},
                  NotificationData.Action("Try Again") {}
                )

              })
            }
          }
          row {
            button("Error") {
              error.set(object : NotificationData {
                override val status: NotificationData.NotificationStatus = NotificationData.NotificationStatus.WARNING
                override val message: String = "ага. тогда сигнал. если решим, что неудобно переделаем"
                override val customActionList: List<NotificationData.Action> = emptyList()

              })
            }
          }


          row {
            button("close dialog") {
              settService.doClose.fire(Unit)
            }
          }
          val syncService = SettingsService.getInstance().getSyncService()
          if(syncService is TestSyncService) {
            row {
              val state = syncService.syncState
                comboBox(SyncService.SYNC_STATE.entries).onChanged {
                  if (it.selectedItem is SyncService.SYNC_STATE) {
                    state.value = it.selectedItem as SyncService.SYNC_STATE
                  }
                }.applyToComponent {
                  selectedItem = state.value
                }
                val component = textField().component
                state.advise(Lifetime.Eternal) {
                  component.text = it.toString()
                }
            }

            row {
              button("Waiting for login") {
                settService.notification.set(object : NotificationData {
                  override val status: NotificationData.NotificationStatus  = NotificationData.NotificationStatus.WAITING
                  override val message: String = IdeBundle.message("login.dialog.waiting.for.login")
                  override val customActionList = listOf(NotificationData.Action("Log in with token..."){},
                                                         NotificationData.Action("Log in with token..."){})
                })


              }
              button("clear") {
                settService.notification.set(null)
              }
            }
            row ("mainProductState") {
              comboBox(TestSyncService.TestState.entries).onChanged {
                if (it.selectedItem is TestSyncService.TestState) {
                  syncService.mainProductState = it.selectedItem as TestSyncService.TestState
                }
              }.applyToComponent {
                selectedItem = syncService.mainProductState
              }
            }
            row ("freshProductState") {
              comboBox(TestSyncService.TestState.entries).onChanged {
                if (it.selectedItem is TestSyncService.TestState) {
                  syncService.freshProductState = it.selectedItem as TestSyncService.TestState
                }
              }.applyToComponent {
                selectedItem = syncService.freshProductState
              }
            }
            row ("oldProductState") {
              comboBox(TestSyncService.TestState.entries).onChanged {
                if (it.selectedItem is TestSyncService.TestState) {
                  syncService.oldProductState = it.selectedItem as TestSyncService.TestState
                }
              }.applyToComponent {
                selectedItem = syncService.oldProductState
              }
            }
          }
        }

        return pane
      }
    }

    dialog.isModal = false
    dialog.isResizable = false
    dialog.show()

    dialog.pack()
  }
}